﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class Admin_AllUser : System.Web.UI.Page
{
    AccessData acc = new AccessData();
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }
    private void BindGrid()
    {
        
        DataTable dt = acc.getTableSProcedure("sp_DanhSachUser");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Find the value in the c04_oprogrs column. You'll have to use

            string value = e.Row.Cells[3].Text;

            if (value == "1")
            {
                e.Row.Cells[3].Text = "Admin";
            }
            else
            {
                if (value == "2")
                {
                    e.Row.Cells[3].Text = "Quản Lý";
                }
                else
                {
                    e.Row.Cells[3].Text = "Thành Viên";
                }
            }
            string kichhoat = e.Row.Cells[4].Text;
            if (kichhoat == "1")
            {
                e.Row.Cells[4].Text = "Đã Kích Hoạt";

            }
            else
            {
                e.Row.Cells[4].Text = "Chưa Kích Hoạt";
                e.Row.BackColor = System.Drawing.Color.Yellow;
                e.Row.ForeColor = System.Drawing.Color.Red;
               
                
            }
            LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
            l.Attributes.Add("onclick", "javascript:return " +
            "confirm('Bạn có muốn xóa tài khoản " +
            DataBinder.Eval(e.Row.DataItem, "Username") + "')");

        }


    }

    protected void GridView1_OnRowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#d62df4';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#a1c0f9';");
            }
            else
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#d62df4';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#eefef0';");
            }
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.BindGrid();
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gr.Cells[1].FindControl("chkSelect");
            if (cb.Checked)
            {
                // do ur job here
                WebMsgBox.Show(gr.RowIndex.ToString());
            }
        }
        
    }
    protected void lbAdmin_Click(object sender, EventArgs e)
    {
        //loc danh sach user theo admin
      
        DataTable dt = acc.getTableSProcedure("sp_DanhSach_TheoQuyen",1);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void lbQuanLy_Click(object sender, EventArgs e)
    {
        //loc danh sach user theo quan ly
       
        DataTable dt = acc.getTableSProcedure("sp_DanhSach_TheoQuyen",2);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void lbThanhvien_Click(object sender, EventArgs e)
    {
        //loc danh sach user theo thanh vien
        
        DataTable dt = acc.getTableSProcedure("sp_DanhSach_TheoQuyen", 3);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void lbAllUser_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void btnLoc_Click(object sender, EventArgs e)
    {
        //xoa
        
    }
    
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            // get the categoryID of the clicked row

            string username = Convert.ToString(e.CommandArgument) ;
            // Delete the record 

            //xoa user 
           
            acc.ExecSProcedure("sp_Delete_User", username);

            BindGrid();
        }
    }

    #region proprerties
    private int? EditCustomerID
    {
        get
        {
            return (int?)ViewState["EditCustomerID"];
        }

        set
        {
            ViewState["EditCustomerID"] = value;
        }
    }

    #endregion
    protected void btnAddCustomer_Click(object sender, EventArgs e)
    {
        WebMsgBox.Show("heello");
    }
     
}